package com.cooba.activitys.Living;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.bean.LivingDetailBean;
import com.cooba.utils.ServerURL;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

public class LivingContentAddActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView my_living_add_back_ib;
    private TextView my_living_add_submit;
    private CheckBox my_living_add_cb;
    private EditText my_living_add_et;
    private HttpUtils httpUtils;
    private RequestParams params;
    private String livie_id;
    private String is_title = "0";
    private LivingDetailBean.LivingDetailContent livingdetailcontentbean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_living_add_content);
        livie_id = getIntent().getStringExtra("live_id");
        //Toast.makeText(this, "直播id:" + livie_id, Toast.LENGTH_SHORT).show();
        initView();
        initListener();
    }

    private void initListener() {
        my_living_add_back_ib.setOnClickListener(this);
        my_living_add_submit.setOnClickListener(this);
    }

    private void initView() {
        my_living_add_submit = (TextView) findViewById(R.id.my_living_add_submit);
        my_living_add_back_ib = (TextView) findViewById(R.id.my_living_add_back_ib);
        my_living_add_cb = (CheckBox) findViewById(R.id.my_living_add_cb);
        my_living_add_et = (EditText) findViewById(R.id.my_living_add_et);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_living_add_back_ib:
                finish();
                break;
            case R.id.my_living_add_submit:
                doSubmit();
                break;
            default:
                break;
        }
    }

    private void doSubmit() {
        httpUtils = new HttpUtils();
        params = new RequestParams();
        if (my_living_add_cb.isChecked()) {
            is_title = "1";
        } else {
            is_title = "0";
        }
        params.addBodyParameter("live_id", livie_id);
        params.addBodyParameter("message_type", "0");
        params.addBodyParameter("is_title", is_title);
        params.addBodyParameter("message", my_living_add_et.getText().toString().trim());
        String urlmylivingadd = ServerURL.httpUrl + ServerURL.mylivingadd;
        httpUtils.send(HttpRequest.HttpMethod.POST, urlmylivingadd, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("wei", "发布内容成功" + responseInfo.result);

                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseInfo.result.toString());
                    String status = jsonObject.get("status").toString();
                    if (status.equals("1")){
//                        livingdetailcontentbean = new Gson().fromJson(responseInfo.result, LivingDetailBean.LivingDetailContent.class);
//                        Intent intent=new Intent();
//                        intent.putExtra("livingdetailcontentbean",livingdetailcontentbean);
                        setResult(RESULT_OK);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.d("wei", "发布内容失败" + msg);
            }
        });
    }
}
